using System;
using Consts;
using Dto;
using Model;
using UnityEngine;
using UnityEngine.UI;

namespace UI
{
    public class ButtomPanel : UIBase
    {
        private Text _txtBeen;
        private Text _txtMutiple;
        private Button _btnChat;
        private Image _imgChoose;
        private Button[] _btns;
        private ReqBase _reqBase;


        private void Awake()
        {
            Bind(UIEvent.CHANGE_MUTIPLE);
        }

        public override void Execute(int eventCode, object message)
        {
            switch (eventCode)
            {
                case UIEvent.CHANGE_MUTIPLE:
                {
                    ChangeMutiple((int) message);
                    break;
                }
                default:
                    break;
            }
        }

        void Start()
        {
            InitPanel();
            _imgChoose.gameObject.SetActive(false);
            if (Models.GameModel.Account != null)
                RefreshPanel(Models.GameModel.Account.been);
        }

        private void InitPanel()
        {
            _txtBeen = transform.Find("TxtBeen").GetComponent<Text>();
            _txtMutiple = transform.Find("TxtMutiple").GetComponent<Text>();
            _btnChat = transform.Find("BtnChat").GetComponent<Button>();
            _btns = new Button[7];
            _imgChoose = transform.Find("ImgChoose").GetComponent<Image>();
            for (int i = 0; i < 7; i++)
            {
                int temp = i + 1;
                _btns[i] = _imgChoose.transform.GetChild(i).GetComponent<Button>();
                _btns[i].onClick.AddListener(delegate { ChatClick(temp); });
            }

            _btnChat.onClick.AddListener(SetChooseActive);
            _reqBase = new ReqBase(SocketConst.CHAT);
        }

        /**
         * 刷新自身面板信息
         */
        private void RefreshPanel(int beenCount)
        {
            this._txtBeen.text = "x " + beenCount;
        }

        /**
         * 改变牌局倍数
         */
        private void ChangeMutiple(int mutiple)
        {
            this._txtMutiple.text = "倍数 x" + mutiple;
        }

        /**
         * 设置选择对话内容的面板显示
         */
        private void SetChooseActive()
        {
            bool active = _imgChoose.gameObject.activeInHierarchy;
            _imgChoose.gameObject.SetActive(!active);
        }

        /**
         * 点击某一句聊天内容时候调用
         */
        private void ChatClick(int chatType)
        {
            _reqBase.Change(ChatCodeConst.DEFAULT, chatType);
            Dispatch(AreaCode.NET, 0, _reqBase);
        }
    }
}